package com.tendory.gpssim.activity;

import android.os.Bundle;
import android.view.ViewStub;

import com.amap.api.maps.AMap;
import com.amap.api.maps.AMapOptions;
import com.amap.api.maps.MapView;
import com.amap.api.maps.UiSettings;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MyLocationStyle;
import com.tendory.gpssim.R;
import com.trello.rxlifecycle3.components.support.RxAppCompatActivity;

import org.jetbrains.annotations.NotNull;

public class MapBaseActivity extends RxAppCompatActivity {
    protected MapView mMapView;
    protected AMap mAMap;
    private Bundle savedInstanceState;
    private boolean needResumeMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.savedInstanceState = savedInstanceState;
    }

    protected void initMapDelay() {
        ViewStub stub = findViewById(R.id.viewstub_map);
        if (stub == null) {
            return;
        }
        stub.inflate();
        initMap();
    }

    protected void initMap() {
        mMapView = findViewById(R.id.map);
        mMapView.onCreate(savedInstanceState);// 此方法必须重写
        if (needResumeMap) {
            mMapView.onResume();
        }
        mAMap = mMapView.getMap();
//        LatLng localLatLng = new LatLng(33.90403, 130.407525);
        //mAMap.moveCamera(CameraUpdateFactory.newLatLng(localLatLng));
    }

    protected void setMapStyle() {
//        CustomMapUtil.setStyle(mAMap);

        UiSettings mUiSettings = mAMap.getUiSettings();
        mUiSettings.setRotateGesturesEnabled(false);
//        mUiSettings.setLogoPosition(AMapOptions.LOGO_POSITION_BOTTOM_RIGHT);// 设置地图logo显示在右下方
//        mUiSettings.setCompassEnabled(true);
        mUiSettings.setScaleControlsEnabled(true);
        mUiSettings.setZoomControlsEnabled(true);
        mUiSettings.setZoomPosition(AMapOptions.ZOOM_POSITION_RIGHT_CENTER);
        int currentPos = mUiSettings.getZoomPosition();
//        mAMap.setMaxAndMinZoomLevel(20, 5);
//        MapStatus mapStatus = new MapStatus.Builder().zoom(18).build();
//        MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mapStatus);
//        mAMap.setMapStatus(mapStatusUpdate);
//        mAMap.setOnMapStatusChangeListener(new BaiduMap.OnMapStatusChangeListener() {
//            @Override
//            public void onMapStatusChangeStart(MapStatus mapStatus) {
//                //mLlLocation.setVisibility(View.GONE);
//            }
//
//            @Override
//            public void onMapStatusChangeStart(MapStatus mapStatus, int i) {
//
//            }
//
//            @Override
//            public void onMapStatusChange(MapStatus mapStatus) {
//
//            }
//
//            @Override
//            public void onMapStatusChangeFinish(MapStatus mapStatus) {
//                LatLng ptCenter = mAMap.getMapStatus().target;
//                setPopupTipsInfo(ptCenter);
//            }
//        });
//
//        mGeoCoder.setOnGetGeoCodeResultListener(new OnGetGeoCoderResultListener() {
//
//            @Override
//            public void onGetGeoCodeResult(GeoCodeResult geoCodeResult) {
//
//            }
//
//            @Override
//            public void onGetReverseGeoCodeResult(ReverseGeoCodeResult reverseGeoCodeResult) {
//                if (reverseGeoCodeResult == null || reverseGeoCodeResult.error != SearchResult.ERRORNO.NO_ERROR) {
////                    Toast.makeText(LocationActivity2.this, "没找到该地址", Toast.LENGTH_SHORT).show();
//                } else {
//                    // 获取大头针的地理位置
////                    mAddress = reverseGeoCodeResult.getAddress();
////                    mDescription = reverseGeoCodeResult.getSematicDescription();
//                    // 获取大头针的坐标
//                    LatLng location = reverseGeoCodeResult.getLocation();
//                    selectLat = location.latitude;
//                    selectLon = location.longitude;
//                    // 显示大头针所在的信息
//                    if (mAnimatorSet == null) {
//                        mAnimatorSet = new AnimatorSet();
//                    }
//                    mAnimatorSet.playTogether(ObjectAnimator.ofFloat(mIvBigpin, "translationY", 0.0f, -30.0f, 0.0f),
//                            ObjectAnimator.ofFloat(mIvBigpin, "rotationY", 0.0f, 720.0f));
//                    mAnimatorSet.setDuration(500);
//                    mAnimatorSet.start();
////                    mTvLocation.setText(mDescription);
////                    mLlLocation.setVisibility(View.VISIBLE);
//                }
//            }
//        });
    }

    protected void setMyLocStyle() {
        MyLocationStyle myLocationStyle = new MyLocationStyle();
        myLocationStyle.myLocationIcon(BitmapDescriptorFactory.fromResource(R.drawable.ico_location));// 设置小蓝点的图标
        myLocationStyle.interval(1000); //设置连续定位模式下的定位间隔，只在连续定位模式下生效，单次定位模式下不会生效。单位为毫秒。
        //myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATE) ;//定位一次，且将视角移动到地图中心点。
        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE_NO_CENTER);//连续定位、蓝点不会移动到地图中心点，定位点依照设备方向旋转，并且蓝点会跟随设备移动。
        myLocationStyle.showMyLocation(true);
//        myLocationStyle.strokeColor();
        myLocationStyle.radiusFillColor(0xA0D8D8D8);
        mAMap.setMyLocationStyle(myLocationStyle);//设置定位蓝点的Style
        mAMap.getUiSettings().setMyLocationButtonEnabled(true);//设置默认定位按钮是否显示，非必需设置。
        mAMap.setMyLocationEnabled(true);// 设置为true表示启动显示定位蓝点，false表示隐藏定位蓝点并不进行定位，默认是false。
//        mAMap.setOnMyLocationChangeListener(new AMap.OnMyLocationChangeListener() {
//            @Override
//            public void onMyLocationChange(Location location) {
//                //从location对象中获取经纬度信息，地址描述信息，建议拿到位置之后调用逆地理编码接口获取（获取地址描述数据章节有介绍）
//            }
//        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mMapView != null) {
            mMapView.onDestroy();
            mMapView = null;
            mAMap.setMyLocationEnabled(false);
        }
    }

    @Override
    public void onSaveInstanceState(@NotNull Bundle outState) {
        super.onSaveInstanceState(outState);
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState)，保存地图当前的状态
        if (mMapView != null) {
            mMapView.onSaveInstanceState(outState);
        }
    }

    @Override
    protected void onRestoreInstanceState(@NotNull Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mMapView != null) {
            mMapView.onResume();
        } else {
            needResumeMap = true;
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mMapView != null) {
            mMapView.onPause();
        }
        needResumeMap = false;
        //取消注册传感器监听
//        mSensorManager.unregisterListener(eventListener);
    }
}
